package com.mercenary.wein.screenControl.service;

import com.mercenary.wein.screenControl.deviceControll.AndroidDevice;
import com.mercenary.wein.screenControl.bean.DeviceBean;
import com.mercenary.wein.screenControl.dao.DeviceDao;
import com.mercenary.wein.screenControl.dao.GroupDao;

import java.util.List;

/**
 * Created by tuxiao on 2017/3/11.
 */
public class DeviceService {
    static DeviceDao deviceDao = new DeviceDao();
    static GroupDao groupDao = new GroupDao();

    public DeviceBean findBySerialNo(String serialNo){
        List<DeviceBean> deviceBean = deviceDao.findBySerialNo(serialNo);
        if(deviceBean==null||deviceBean.size()==0){
            return null;
        }
        return deviceBean.get(0);
    }

    public DeviceBean registerDevice(AndroidDevice androidDevice){
        List<DeviceBean> deviceBean = deviceDao.findBySerialNo(androidDevice.getSerialNo());
        if(deviceBean==null){
            int port  = Math.max(deviceDao.getMaxPort(),1299)+1;//端口从1300开始
            DeviceBean dbean = deviceDao.register(androidDevice.getSerialNo(),port,androidDevice.getManufacturer(),androidDevice.getProductModel(),androidDevice.getSerialNo(),0);
            groupDao.insertDeviceGroup(-1,androidDevice.getSerialNo());
            setDeviceOnline(androidDevice.getSerialNo());
            return dbean;
        }
        setDeviceOnline(deviceBean.get(0).getSerialNo());
        return deviceBean.get(0);
    }

    public void resetDeviceStatus(){
        deviceDao.resetDeviceStatus();
    }

    public void setDeviceOnline(String serialNo){
        deviceDao.setDeviceStatus(serialNo,0);
    }

    public void setDeviceOffline(String serialNo){
        deviceDao.setDeviceStatus(serialNo,1);
    }
}
